import { observable, action,toJS } from "mobx";
import * as API from "../apis/contentExport";
import { message } from "antd";
import { WeaForm,WeaTableNew} from "comsMobx";
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
export class BlogContentExportStore {
  @observable
  status = {
    loading:false,
    conditionInfo: []
  };
  @observable alertPage=false;
  @observable loading = true; //加载中
  @observable radioGroupParams={};

  @observable selectedKeys = [];
  @observable submitStatus='1';
  @observable exportSortListIsShow=false;
  @observable exportPermissionsListIsShow=false;
  @observable tableStore = new TableStore();
  @observable tableStore2 = new TableStore();
  setExportListSortIsShow = (bool) => this.exportSortListIsShow = bool;
  setExportPermissionsListIsShow = (bool) => this.exportPermissionsListIsShow = bool;
  setSelectedKeys = (keys) => this.selectedKeys = keys;
  @action
  doInit = () => {
    this.getBlogBackstageExportCondition();
    this.getTableSortDatas();
  };
  @observable sortData = [];
  @action("初始化数据")
  updateInitData = (data = []) => {
    this.sortData = data;
  }

  //应内容导出condition
  getBlogBackstageExportCondition = () => {
    this.status.loading=true;
    API.getBlogBackstageExportCondition().then(result => {
      if(result.viewRight == 0){
        this.alertPage = true;
      }else{
        this.alertPage = false;
      }
      this.status.conditionInfo = result.conditioninfo;
     
      this.radioGroupParams = toJS(result.radioGroupParams);
      this.status.loading=false;
    });
  };

  @action("渲染操作日志数据")
  getTableDatas = (params) => {
      this.loading = true;
      if(!this.radioGroupParams.submitStatus){
        params.abnormal="1";
      }
      API.exportBlogContentList(params).then(action(res => {
          if (res.api_status) {
              this.tableStore.getDatas(res.datas, 1);
          } else {
              message.error(res.msg || getLabel('508818', '接口调用失败！'))
          }
          this.loading = false;
      }));
  };

  @action("渲染操作日志数据")//未提交
  getTableDatas2 = (params) => {
      this.loading = true;
      API.exportBlogContentList(params).then(action(res => {
          if (res.api_status) {
              this.tableStore2.getDatas(res.datas, 1);
          } else {
              message.error(res.msg || getLabel('508818', '接口调用失败！'))
          }
          this.loading = false;
      }));
  };

  @action("获取排序数据")
  getTableSortDatas = () => {
      API.getTableSortDatas().then(action(res => {
          if (res.status) {
              this.selectedKeys = res.selectedKeys;
              this.sortData = res.sortData;
          } else {
              message.error(res.msg || getLabel('508818', '接口调用失败！'))
          }
      }));
  };
  @action("获取修改数据")
  updateExportSort= (data) => {
    this.exportSortListIsShow = false;
    API.updateExportSort(data).then(action(res => {
      if (res.status) {
        if(this.submitStatus=='3'){
           this.getTableDatas2(this.radioGroupParams);
        }else{
           this.getTableDatas(this.radioGroupParams);
        }
      } else {
          message.error(res.msg || getLabel('508818', '接口调用失败！'))
      }
    }));
  }

  @action('显示列定制')
  showColumn = () => {
      this.tableStore.setColSetVisible(true);
      this.tableStore.tableColSet(true);
  };

  @action('显示列定制')
  showColumn2 = () => {
      this.tableStore2.setColSetVisible(true);
      this.tableStore2.tableColSet(true);
  };

  @observable rightvalue = "";
  @action updateRightValue = (str = "") => {
      this.rightvalue = str;
  }

  @observable leftvalue = "";
  @action updateLeftValue = (str="") => {
      this.leftvalue = str;
  }




  //应内容导出
  operateBlogBackstageExport = (params) => {
    let dataKey = this.tableStore.dataKey;
    if(this.submitStatus=='3'){
      dataKey = this.tableStore2.dataKey;
    };
    API.getExportColumn({dataKey:dataKey}).then(result => {
      let columns = "";
      let sData = toJS(result.destdatas);
      sData.map(item=>{
          columns+=item.dbField+",";
      });
      params.columns = columns;
      API.operateBlogBackstageExport(params).then(result => {
        window.location="/weaver/weaver.file.ExcelOut?setWrapText=1";
      });
    });

   
  };

  updateRadioGroupParams = (params) => {
    if(typeof(params.userRange)!="undefined" && params.userRange!=toJS(this.radioGroupParams.userRange)){
      this.radioGroupParams.userid='';
      this.radioGroupParams.department='';
      this.radioGroupParams.subdepartment='';
      this.status.conditionInfo[1].selectLinkageDatas[2].browserConditionParam.replaceDatas = []
    }
    for(let key  in params){
      this.radioGroupParams[key] = params[key];
    }
    let radioGroupParams = this.radioGroupParams;
    if(radioGroupParams.submitStatus=='3'){
      this.submitStatus=radioGroupParams.submitStatus;
      this.getTableDatas2(radioGroupParams);
    }else{
      this.submitStatus=radioGroupParams.submitStatus;
      this.getTableDatas(radioGroupParams);
    }
  };
}
